package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.rfid.RfidEventOldmanDealDto;
import com.csun.cmny.provider.model.dto.rfid.RfidEventOldmanQueryDto;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidEventOldmanVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcRfidEventOldmanService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-24 11:52
 * @Description: the class dmc rfid event oldman controller
 */
@RestController
@RequestMapping(value = "/rfidEventOldman", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcRfidEventOldmanController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcRfidEventOldmanService dmcRfidEventOldmanService;

    /**
     * 查询老人外出通知列表 - 分页查询
     *
     * @param rfidEventOldmanQueryDto   the rfid event oldman query dto
     *
     * @return  the wrapper.
     */
    @PostMapping("/queryOldmanOutNotifyListWithPage")
    public Wrapper queryOldmanOutNotifyListWithPage(@RequestBody RfidEventOldmanQueryDto rfidEventOldmanQueryDto) {

        rfidEventOldmanQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        rfidEventOldmanQueryDto.setNumber(0);
        rfidEventOldmanQueryDto.setOrderBy("created_time desc");

        List<DmcRfidEventOldmanVo> dmcRfidEventOldmanVoList = dmcRfidEventOldmanService
                .queryListWithPage(rfidEventOldmanQueryDto);

       // return WrapMapper.ok(new PageInfo<>(dmcRfidEventOldmanVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(dmcRfidEventOldmanVoList));
    }

    /**
     * 查询老人外出记录列表 - 分页查询
     *
     * @param rfidEventOldmanQueryDto   the rfid event oldman query dto
     *
     * @return the wrapper.
     */
    @PostMapping("/queryOldmanOutRecordListWithPage")
    public Wrapper queryOldmanOutRecordListWithPage(@RequestBody RfidEventOldmanQueryDto rfidEventOldmanQueryDto) {

        rfidEventOldmanQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        rfidEventOldmanQueryDto.setDealStat(1);
        rfidEventOldmanQueryDto.setDealResult(1);
        rfidEventOldmanQueryDto.setMatchStat(1);
        rfidEventOldmanQueryDto.setNumber(1);
        rfidEventOldmanQueryDto.setOrderBy("created_time desc");

        List<DmcRfidEventOldmanVo> dmcRfidEventOldmanVoList = dmcRfidEventOldmanService
                .queryListWithPage(rfidEventOldmanQueryDto);

       // return WrapMapper.ok(new PageInfo<>(dmcRfidEventOldmanVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(dmcRfidEventOldmanVoList));
    }

    /**
     * 处理老人外出通知
     *
     * @param rfidEventOldmanDealDto    the rfid event oldman deal dto
     * @param br    the binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @PostMapping("/dealOldmanOutNotify")
    public Wrapper dealOldmanOutNotify(@Validated @RequestBody RfidEventOldmanDealDto rfidEventOldmanDealDto,
                                       BindingResult br) {

        Integer result = dmcRfidEventOldmanService.dealOldmanOutNotify(rfidEventOldmanDealDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
